package org.nch.videoLauncher.view;

import org.nch.videoLauncher.domain.VideoItem;
import org.nch.videoLauncher.presenter.SearchPresenter;

import javax.swing.*;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import java.util.List;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import com.jgoodies.forms.layout.CellConstraints;

/**
 * Created by IntelliJ IDEA.
 * User: bill
 * Date: Jan 8, 2009
 * Time: 3:44:29 PM
 * To change this template use File | Settings | File Templates.
 */
public class SearchView implements ISearchView, IContentPanel{
    private JPanel mainPanel;
    private JTabbedPane searchTabPane;
    private JPanel paramsTab;
    private JPanel resultsTab;
    private JPanel quickSearchPanel;
    private JPanel movieSearchPanel;
    private JPanel tvSearchPanel;
    private JPanel sportsSearchPanel;
    private JTextField quickSearchText;
    private JLabel searchForLabel;
    private JLabel quickSearchHelpLabel;
    private JButton quickSearchButton;
    private JTextField movieDirectorText;
    private JTextField movieRatingText;
    private JTextField movieCastText;
    private JTextField movieGenreText;
    private JButton searchMoviesButton;
    private JTextField tvShowText;
    private JTextField tvSeasonText;
    private JTextField tvEpisodeText;
    private JButton tvSearchButton;
    private JTextField sportSportText;
    private JTextField sportTeamText;
    private JTextField sportSeasonText;
    private JButton sportsSearchButton;
    private JLabel sportSportLabel;
    private JLabel sportTeamLabel;
    private JLabel sportTeamSeason;
    private JLabel directorLabel;
    private JLabel castLabel;
    private JLabel genreLabel;
    private JLabel showLabel;
    private JLabel tvseasonLabel;
    private JLabel episodeLabel;
    private JPanel listPanel;
    private JPanel detailsPanel;
    private JScrollPane listScrollPane;
    private JList resultsList;

    private Integer currentListRow;
    private VideoItem currentVideoItem;
    private DefaultListModel listModel;
    private SearchPresenter presenter;

    public SearchView(){

        currentListRow = -1;

        resultsList.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent event) {
                if (event.getValueIsAdjusting())
                        return;

                if (listModel.size() == 0)
                        return;

                if (!resultsList.isSelectionEmpty()) {

                        presenter.selectionChanged();
                        currentListRow = resultsList.getSelectedIndex();
                } 
            }
        });

        listModel = new DefaultListModel();
        resultsList.setModel(listModel);
        quickSearchButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                presenter.quickSearch();
            }
        });
        quickSearchText.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                presenter.quickSearch();
            }
        });
        searchMoviesButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                presenter.movieSearch();
            }
        });
        tvSearchButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                presenter.tvSearch();
            }
        });
        sportsSearchButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                presenter.sportsSearch();
            }
        });
    }

    public void setPresenter(SearchPresenter presenter) {
        this.presenter = presenter;
    }

    public void showResultsTab() {
        searchTabPane.setEnabledAt(1, true);
        searchTabPane.setSelectedIndex(1);
    }

    public void showParametersTab() {
        searchTabPane.setSelectedIndex(0);
    }

    public void clearResultsList() {

        detailsPanel.removeAll();
        listModel = null;
        listModel = new DefaultListModel();
        resultsList.setModel(listModel);


    }

    public String getQuickSearchText() {
        return quickSearchText.getText();
    }

    public void setQuickSearchText(String searchTerm) {
        quickSearchText.setText(searchTerm);
    }

    public String getShowText() {
        return tvShowText.getText();
    }

    public String getTvSeasonText() {
        return tvSeasonText.getText();
    }

    public String getEpisodeText() {
        return tvEpisodeText.getText();
    }

    public String getSportText() {
        return sportSportText.getText();
    }

    public String getSportSeasonText() {
        return sportSeasonText.getText();
    }

    public String getTeamText() {
        return sportTeamText.getText();
    }

    public String getDirectorText() {
        return movieDirectorText.getText();
    }

    public String getCastText() {
        return movieCastText.getText();
    }

    public String getGenreText() {
        return movieGenreText.getText();
    }

    public JPanel getRootComponent() {
        return mainPanel;
    }

    public Integer getCurrentSelection() {
        return resultsList.getSelectedIndex();
    }

    public void addListElement(String name){
        listModel.addElement(name);
    }    

    public void showDetailsView(IContentPanel detailsView) {

        detailsPanel.removeAll();
        CellConstraints cc = new CellConstraints();
        detailsPanel.add(detailsView.getRootComponent(), cc.xy(1,1));
        detailsPanel.updateUI();    // tell (jgoodies) f        
    }

    public void quickSearch(){
        presenter.quickSearch();
    }

    public void setCurrentSelection(Integer index){
        resultsList.getSelectionModel().setSelectionInterval(index, index);
        currentListRow = index;        
    }

}
